package com.mgetool.ui.animation;

import java.util.Vector;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

import com.mgetool.ui.AbstractDrawable;
import com.mgetool.ui.DrawableInterface;
import com.mgetool.ui.drawer.BitmapDrawerParam;



/**
 * 组成帧的图片切块类 0张或者n张图片切块组成1帧
 * @author wangbin
 *
 */
public class ImageSplitTile extends AbstractDrawable{

//	private IBitmap ibitmap;
//	private IBitmapDrawable bitmap;
	
	private Point offset;
	private BitmapDrawerParam drawerParam;
	private DrawableInterface drawable;
	public ImageSplitTile(DrawableInterface drawable) {
		setDrawable(drawable);
	}
	
	public DrawableInterface getDrawable(){
		return drawable;
	}
	
	public void setDrawable(DrawableInterface drawable){
		this.drawable = drawable;
		if(drawable != null){
			setSize(drawable.getWidth(), drawable.getHeight());
		}
	}
	
//	public ImageSplitTile(IBitmap bitmap) {
//		ibitmap = bitmap;
//		if(ibitmap != null){
//			setSize(ibitmap.getWidth(), ibitmap.getHeight());
//		}
//	}
	
//	public ImageSplitTile(IBitmapDrawable drawable) {
//		this.bitmap = drawable;
//		if(bitmap != null){
//			setSize(bitmap.getWidth(), bitmap.getHeight());
//		}
//	}
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 得到帧的宽度
	 * @return
	 */
	public int getWidth(){
//		if(getBitmap() != null){
//			return getBitmap().getWidth();
//		}
//		if(getIBitmap() != null){
//			return getIBitmap().getWidth();
//		}
		if(getDrawable() != null){
			return getDrawable().getWidth();
		}
		return 0;
	}
	
	/**
	 * 得到帧的高度
	 * @return
	 */
	public int getHeight(){
//		if(getBitmap() != null){
//			return getBitmap().getHeight();
//		}
//		if(getIBitmap() != null){
//			return getIBitmap().getHeight();
//		}
		if(getDrawable() != null){
			return getDrawable().getHeight();
		}
		return 0;
	}
	
	@Override
	public void onDraw(GC gc) {
		// TODO Auto-generated method stub
//		if(getBitmap() != null){
//			bitmap.setDrawerParam(drawerParam);
//			bitmap.onDraw(canvas, getX(), getY(), paint);
//		}else if(getIBitmap() != null){
//			if(drawerParam != null){
//				getIBitmap().draw(canvas, getX(), getY(), paint,
//						drawerParam.getRotate(),
//						drawerParam.getScaling(),
//						drawerParam.getAlpha());
//			}else{
//				getIBitmap().draw(canvas, getX(), getY(), paint);
//			}
//		}
		if(getDrawable() != null){
			getDrawable().setDrawerParam(drawerParam);
			getDrawable().onDraw(gc, getX(), getY());
		}
	}

	public void setDrawerParam(BitmapDrawerParam drawerParam){
		this.drawerParam = drawerParam;
	}
	
	private BitmapDrawerParam getParam(){
//		if(getBitmap() != null){
//			return ((BitmapDrawer)getBitmap().getDrawer()).getParam();
//		}else if(getIBitmap() != null){
//			return ((BitmapDrawer)getIBitmap().getDrawer()).getParam();
//		}
		if(getDrawable() != null && getDrawable().getDrawer() != null){
			return getDrawable().getDrawer().getParam();
		}
		return null;
	}
	
	@Override
	public void onDraw(GC gc, int x, int y) {
		// TODO Auto-generated method stub
		int xoff = x;
		int yoff = y;
		if(getOffset() != null){
			if(getParam() != null && getParam().getScaling() != null ){
				xoff += getOffset().x * getParam().getScaling().getScallingX();
				yoff += getOffset().y * getParam().getScaling().getScallingY();
			}else{
				xoff += getOffset().x;
				yoff += getOffset().y;
			}
		}
		super.onDraw(gc, xoff, yoff);
	}


	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}
	
//	public IBitmapDrawable getBitmap(){
//		return bitmap;
//	}
	
//	public IBitmap getIBitmap(){
//		return ibitmap;
//	}
	
	public Point getOffset() {
		return offset;
	}

	public void setOffset(Point offset) {
		this.offset = offset;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
//		if(bitmap != null){
//			bitmap.onDestroy();
//			bitmap = null;
//		}
		if(drawable != null){
			drawable.onDestroy();
			drawable = null;
		}
		super.onDestroy();
	}

	@Override
	public boolean onKey(int eventState, KeyEvent keyEvent) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onMouseEvent(int eventState, MouseEvent event) {
		// TODO Auto-generated method stub
		return false;
	}
	
}
